package ua.pp.shell.wka;

import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import ua.pp.shell.wka.DBAdapter.Types.VocabularyStatistic;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class TeacherWidget extends AppWidgetProvider {
	public static String ACTION_WIDGET_ACTION_TEACH = "teach";
	public static String ACTION_WIDGET_ACTION_ADD_WORD = "add_word";
	public static String ACTION_WIDGET_ACTION_ADD_VOCABULARY = "add_vocabulary";
	public static String ACTION_WIDGET_ACTION_LOOK_VOCABULARY = "look_vocabulary";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Settings settings = new Settings(context);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.teacher_widget);
		Intent intent;
		PendingIntent configPendingIntent;
		int id_vocabulary = settings.GetCurrentVocabulary();
		//If any vocabulary is not created yet.
		if (id_vocabulary < 0) {
			intent = new Intent(context, TeacherActivity.class);
			intent.setAction(ACTION_WIDGET_ACTION_TEACH);
			intent = new Intent(context, VocabularyListActivity.class);
			intent.setAction(ACTION_WIDGET_ACTION_ADD_VOCABULARY);
			configPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			remoteViews.setOnClickPendingIntent(R.id.teach, configPendingIntent);
			remoteViews.setOnClickPendingIntent(R.id.vocabulary_title, configPendingIntent);
			remoteViews.setOnClickPendingIntent(R.id.add_word, configPendingIntent);
			remoteViews.setTextViewText(R.id.words, "0");
			remoteViews.setTextViewText(R.id.knowledge, "0%");
			return;
		}
		//elsewhere
		DBAdapter dbAdapter = new DBAdapter(context);
		VocabularyStatistic statistic = dbAdapter.GetVocabularyStatistic(id_vocabulary);
		Vocabulary vocabulary = dbAdapter.GetVocabulary(id_vocabulary);
		remoteViews.setTextViewText(R.id.vocabulary_title, vocabulary.title);
		remoteViews.setTextViewText(R.id.words, Integer.toString(statistic.words));
		remoteViews.setTextViewText(R.id.knowledge, Integer.toString(statistic.percentage) + "%");
		//
		intent = new Intent(context, TeacherActivity.class);
		intent.setAction(ACTION_WIDGET_ACTION_TEACH);
		configPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.teach, configPendingIntent);
		//
		intent = new Intent(context, WordListActivity.class);
		intent.setAction(ACTION_WIDGET_ACTION_LOOK_VOCABULARY);
		configPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.vocabulary_title, configPendingIntent);
		//
		intent = new Intent(context, AEWordActivity.class);
		intent.setAction(ACTION_WIDGET_ACTION_ADD_WORD);
		configPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		remoteViews.setOnClickPendingIntent(R.id.add_word, configPendingIntent);
		//
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
	}
}
 